import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store/index.js'; // 引入 Vuex 状态管理

Vue.use(Router)

const router = new Router({
  routes: [
    //测试不同角色入口
    {
      path: '/',
      name: 'Home',
      component: () => import('@/views/Home'),
      meta: { requiresAuth: true }
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/Login')
      }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    const mId = window.sessionStorage.getItem('mId');
    console.log(mId)
    console.log(store.state.isLoggedIn)
    if (!store.state.isLoggedIn) {
      next('/login');
    } else {
      if(!mId){
        next('/login');
      }else{
        next();
      }
    }
  } else {
    next();
  }
});

export default router;
